package cn.cytong.nk0713.utils;

import cn.cytong.nk0713.utils.nutz.CustomisedNutDao;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Chain;
import org.nutz.dao.entity.Record;

import java.math.BigDecimal;
import java.util.Date;


public class OperationLogUtil {
    public static final int Operation_Type_Other = 0; // 其它
    public static final int Operation_Type_Recharge = 1; // 信用充值
    public static final int Operation_Type_Allocation = 2; // 信用分配
    public static final int Operation_Type_Active = 3; // 激活
    public static final int Operation_Type_Inactive = 4; // 取消激活


    /**
     * 写日志
     *
     * @param operator
     * @param type
     * @param describe
     */
    public static void info(Record operator, int type, String describe, Record targetProvider, Record targetBranch, Record targetOperator, int credit, CustomisedNutDao nutDao) {
        Integer targetType = 0;
        Long targetProvider_id = null, targetBranch_id = null, targetOperator_id = null;
        if (targetProvider != null) {
            targetProvider_id = (Long) targetProvider.get("id");
            targetType = CreditUtil.TYPE_PROVIDER;
        }
        if (targetBranch != null) {
            targetType = CreditUtil.TYPE_BRANCH;
            targetBranch_id = (Long) targetBranch.get("id");
            targetProvider_id = (Long) targetBranch.get("serviceProvider_id"); // 额外记录
        }
        if (targetOperator != null) {
            targetType = CreditUtil.TYPE_OPERATOR;
            targetProvider_id = (Long) targetOperator.get("serviceProvider_id"); // 额外记录
            targetBranch_id = (Long) targetOperator.get("serviceBranch_id"); // 额外记录
            targetOperator_id = (Long) targetOperator.get("id");

        }
        if (operator == null || StringUtils.isBlank(describe)) {
            return;
        }

        nutDao.insert("OperationLog", Chain.make("operator_id", operator.get("id"))
                .add("type", type)
                .add("status", 1)
                .add("describe", describe)
                .add("operationAt", new Date())
                .add("credit", BigDecimal.valueOf(credit))
                .add("targetType", targetType)
                .add("targetProvider_id", targetProvider_id)
                .add("targetBranch_id", targetBranch_id)
                .add("targetOperator_id", targetOperator_id));

        /*OperationLog log = new OperationLog();
        log.operator = operator;
        log.type = type;
        log.status = 1;
        log.describe = describe;
        log.credit = BigDecimal.valueOf(credit);
        log.targetType = targetType;
        log.targetProvider = targetProvider;
        log.targetBranch = targetBranch;
        log.targetOperator = targetOperator;
        log.operationAt = new Date();
        log.save();*/
    }

    /**
     * 写错误日志
     *
     * @param operator
     * @param type
     * @param describe
     */
    public static void error(Record operator, int type, String describe, CustomisedNutDao nutDao) {
        if (operator == null || StringUtils.isBlank(describe)) {
            return;
        }

        nutDao.insert("OperationLog", Chain.make("operator_id", operator.get("id"))
                .add("type", type)
                .add("status", 0)
                .add("describe", describe)
                .add("operationAt", new Date()));

        /*OperationLog log = new OperationLog();
        log.operator = operator;
        log.type = type;
        log.status = 0;
        log.describe = describe;
        log.operationAt = new Date();
        log.save();*/
    }

    public static String getBalanceInfo(Record operator, CustomisedNutDao nutDao) {
        String ret = "";
        String providerCredit = "-";
        String branchCredit = "-";
        String operatorCredit = "-";

        Record provider = nutDao.fetchRecord("select * from serviceProvider where id = ?", operator.get("serviceProvider_id"));
        Record branch = nutDao.fetchRecord("select * from serviceBranch where id = ?", operator.get("serviceBranch_id"));

        if (provider != null && provider.get("creditBalance") != null) {
            providerCredit = provider.getString("creditBalance");
        }

        if (branch != null && branch.get("creditBalance") != null) {
            branchCredit = branch.getString("creditBalance");
        }

        if (operator.get("creditBalance") != null) {
            operatorCredit = operator.getString("creditBalance");
        }

        ret = "（操作前信用余额 - 服务商：" + providerCredit + " | 网点：" + branchCredit + " | 操作员：" + operatorCredit + "）";

        return ret;
    }
}
